package com.t3h.lab5;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SharedpreferenceActivity extends Activity {
    private Button mButton;
    public static final String MY_PREF = "preffile";

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText mEdittext = (EditText)findViewById(R.id.edtxt1);
        mButton = (Button) findViewById(R.id.btn1);
         mButton.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences mSp = getSharedPreferences(MY_PREF,MODE_WORLD_READABLE);
				SharedPreferences.Editor editor = mSp.edit();
				editor.putString("Ten", mEdittext.getText().toString());
				editor.commit();
				mSp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
					
					@Override
					public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
							String key) {
						Log.i("TTTH", "onSharedPreferenceChanged");
						
					}
				});
				String mString= mSp.getString("Ten", "");
				TextView mTv2 = (TextView) findViewById(R.id.textView1);
		        mTv2.setText("My old value is : "+mString);
		        
				Intent iIntent = new Intent(SharedpreferenceActivity.this, Mynewactivity.class);
				startActivity(iIntent);
			}
		});
        
        
    }
}